package com.dropgame.screens;

import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenManager;
import aurelienribon.tweenengine.equations.Bounce;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.dropgame.main.AssetLoader;
import com.dropgame.main.DropGame;
import com.dropgame.tools.ImageButton;
import com.dropgame.tools.ImageButton2;
import com.dropgame.tools.ImageButtonAccessor;
import com.dropgame.tools.InvertedInput;
import com.dropgame.tools.MyToastTest;
import com.dropgame.tools.SpriteButton;
import com.dropgame.tools.SpriteButtonAccessor;
import com.dropgame.tools.TransitionEffect;

public class SecondMenuScreenTE implements Screen{

	private static final int VIRTUAL_WIDTH = 320;
    private static final int VIRTUAL_HEIGHT = 480;
    private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
	
	private DropGame game;
	private OrthographicCamera guiCam;
	private TextureRegion gameBackGround;
	
	private SpriteBatch batcher;
	
	private Sprite farmLand;
	private Sprite toastScreen;
	
	private Boolean showToast = false;
	
	private Input input;
	
	private TransitionEffect transitionEffect;
	
	private TweenManager manager;
	
	private ImageButton playButton;
	private ImageButton optionsButton;
	private ImageButton exitButton;
	private ImageButton profileButton;
	
	private ImageButton yesButton;
	private ImageButton noButton;
	
    public Rectangle viewport;
    
    private Timeline firstTime;
    
    private MyToastTest myTest;
    private Boolean showMyToast = false;
    
    float plusDelta;
	
	public SecondMenuScreenTE(DropGame game){
		this.game = game;
		
		// Create the camera with the virtual width and height.

		this.guiCam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);	
		
		 //Set the camera to point at the center of the screen.
		guiCam.position.set(VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2, 0);
		
		// Create viewport to be used in resizing etc.
		// The viewport is initially set to 0.0f but will be resized in resize() when screen is created.
		// This is done to prevent null pointer exceptions happening in various classes that use the viewport.
		this.viewport = new Rectangle(0.0f,0.0f,0.0f,0.0f);
		
		// Create the input.
		this.input = Gdx.input;
		
		// Create the SpriteBatch that will be used to draw our assets.
		this.batcher = new SpriteBatch();
		
		// Create the transitionEffect that is used to display an fade in and out effect when changing screens.
		this.transitionEffect = new TransitionEffect(game);
		
		// Create the transitionEffect that is used to display an fade in and out effect when changing screens.
		this.manager = new TweenManager();
		
		this.gameBackGround = AssetLoader.gameBackgroundRegion;
		
		this.farmLand = AssetLoader.grassLand;
		this.toastScreen = AssetLoader.blueToast;
		toastScreen.setPosition(30.0f, 30.0f);
		
		// Create the arrays for the imageButtons.
		Sprite[] playArray = {AssetLoader.playButton1, AssetLoader.playButton2, AssetLoader.playButton3, AssetLoader.playButton4, AssetLoader.playButton5};
		Sprite[] optionsArray = {AssetLoader.optionsButton};
		Sprite[] exitArray = {AssetLoader.exitButton};
		Sprite[] profileArray = {AssetLoader.profileButton};
		
		Sprite[] yesArray = {AssetLoader.yesButton};
		Sprite[] noArray = {AssetLoader.noButton};
		
		// Create the imageButtons
		this.playButton = new ImageButton(0.15f, playArray, input, guiCam, viewport, 10.0f, 250.0f);
		this.optionsButton = new ImageButton(0.15f, optionsArray, input, guiCam, viewport, 165.0f, 250.0f);
		this.exitButton = new ImageButton(0.15f, exitArray, input, guiCam, viewport, 165.0f, 85.0f);
		this.profileButton = new ImageButton(0.15f, profileArray, input, guiCam, viewport, 10.0f, 85.0f);
		
		this.yesButton = new ImageButton(0.15f, yesArray, input, guiCam, viewport, 0.0f, 0.0f);
		yesButton.setAcceptInput(false);
		this.noButton = new ImageButton(0.15f, noArray, input, guiCam, viewport, 0.0f, 0.0f);
		noButton.setAcceptInput(false);
		
		Tween.registerAccessor(ImageButton.class, new ImageButtonAccessor());
		//Tween.set(playButton, ImageButtonAccessor.POSITION_XY).target(10.0f, 550).start(manager);
		//Tween.to(realSpriteButton, SpriteButtonAccessor.POSITION_XY, 1.0f).target(75, 375).ease(Bounce.OUT).start(manager);
		Timeline.createSequence().push(Tween.set(playButton, ImageButtonAccessor.POSITION_XY).target(10, 550))
								 .push(Tween.to(playButton, ImageButtonAccessor.POSITION_XY, 0.5f).target(10, 250)).start(manager);
		
		
		// Create the toast for exiting the game.
		myTest = new MyToastTest(yesButton, noButton, toastScreen);
		
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render(float deltaTime) {
		// Get an instance of the graphics
		GL10 gl = Gdx.app.getGraphics().getGL10();
		guiCam.update();
		guiCam.apply(gl);
		
		gl.glViewport((int) viewport.x, (int) viewport.y,
          	  		  (int) viewport.width, (int) viewport.height);
		
		// Clear the screen for the next frame to be drawn.
		gl.glClearColor(0, 0, 0, 0);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		batcher.setProjectionMatrix(guiCam.combined);
		batcher.begin();
		//batcher.draw(gameBackGround, 0, 0);
		farmLand.draw(batcher);
		playButton.getKeyFrame(deltaTime).draw(batcher);
		optionsButton.getKeyFrame(deltaTime).draw(batcher);
		exitButton.getKeyFrame(deltaTime).draw(batcher);
		profileButton.getKeyFrame(deltaTime).draw(batcher);
		
		myTest.update(deltaTime, batcher);
		
		
		batcher.end();
		
		transitionEffect.movingScreen(batcher, deltaTime);
		
		
		manager.update(deltaTime);
		
		updateLogic(deltaTime);
		
	
		
	}
	
	
	public void updateLogic(float deltaTime){
		if(playButton.getCompleted()){
			transitionEffect.nextScreen(new FreeOrStoryScreen(game));
		}
		
		if(optionsButton.getCompleted()){
			transitionEffect.nextScreen(new OptionsScreen(game));
		}
		
		if(profileButton.getCompleted()){
			transitionEffect.nextScreen(new ProfileScreen(game));
		}
		
		if(exitButton.getCompleted()){
			myTest.openScreen();
		}
		
		if(myTest.noButton.getCompleted()){
			myTest.closeScreen();
					
		}
		
		if(myTest.yesButton.getCompleted()){
			game.multipleProfile.persistAll();
			Gdx.app.exit();
		}
	}

	@Override
	public void resize(int width, int height) {
		float aspectRatio = (float)width/(float)height;
        float scale = 1f;
        Vector2 crop = new Vector2(0f, 0f);
        if(aspectRatio > ASPECT_RATIO)
        {
            scale = (float)height/(float)VIRTUAL_HEIGHT;
            crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
        }
        else if(aspectRatio < ASPECT_RATIO)
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
            crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
        }
        else
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
        }

        float w = (float)VIRTUAL_WIDTH*scale;
        float h = (float)VIRTUAL_HEIGHT*scale;
        
        // Improved viewport resize
         //viewport = new Rectangle(crop.x, crop.y, w, h);
        viewport.set(crop.x, crop.y, w, h);
        
        //cameraSpriteButton.viewport = viewport;
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

}
